<template>
  <div class="box">
    <el-form size="medium"  ref="itemForm" :model="obj" :rules="itemRules" label-width="100px">
      <el-form-item>
        <span>易联云K4.0</span>
      </el-form-item>
      <el-form-item label="打印机名称" prop="name">
        <el-input placeholder="请输入打印机名称" v-model="obj.name"></el-input>
      </el-form-item>

      <el-form-item label="终端号" prop="printer_no">
        <el-input placeholder="请输入终端号" v-model="obj.printer_no"></el-input>
      </el-form-item>

      <el-form-item label="应用ID" prop="client_id">
        <el-input placeholder="请输入应用ID" v-model="obj.client_id"></el-input>
      </el-form-item>

      <el-form-item label="应用秘钥" prop="client_secret">
        <el-input placeholder="请输入应用秘钥" v-model="obj.client_secret"></el-input>
      </el-form-item>

      <el-form-item label="打印机秘钥">
        <el-input placeholder="请输入打印机秘钥" v-model="obj.printer_secret"></el-input>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" @click="saveData">保存数据</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
import { 
  addReceiptPrinter,
  editReceiptPrinter,
  getReceiptPrinter
} from '@/api/shop/print';

export default {
  name: 'receiptPrinterItem',
  data() {
    return {
      isLoading: false,
      isEdit:false,
      obj:{  
        name:'',
        printer_no:'',
        client_id:'',
        client_secret:'',
        printer_secret:''
      },
      itemRules: {
        name: [{
          required: true,
          message: '请输入打印机名称'
        }],
        printer_no: [{
          required: true,
          message: '请输入终端号'
        }],
        client_id: [{
          required: true,
          message: '请输入应用ID'
        }],
        client_secret: [{
          required: true,
          message: '请输入应用秘钥'
        }]
      }
    }
  },  
  methods: {
    //初始化
    init(id){
      this.isEdit = true;
      this.isLoading = true;
      getReceiptPrinter(id).then(res => {
        this.isLoading = false;
        this.obj = {...res.msg};
        //单独赋值终端号,后端把名字改了
        //this.obj.code = res.msg.printer_no;
      }).catch(e => {
        this.isLoading = false;
      })
    },
    //保存数据(修改发货地址)
    saveData(){
      this.$refs.itemForm.validate(valid => {
        if (valid) {
          let o = {...this.obj};

          this.isLoading = true;
          if(this.isEdit){
            //编辑
            editReceiptPrinter(o,o.id).then(res => {
              this.isLoading = false;
              this.$emit('success');
            }).catch(e => {
              this.isLoading = false;
            })
          }else{
            //新增
            addReceiptPrinter(o).then(res => {
              this.isLoading = false;
              this.$emit('success');
            }).catch(e => {
              this.isLoading = false;
            });
          }
        } else {
          //console.log('error')
          return false
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item{
  margin-bottom:15px;
}
</style>
